package kr.totalcall.util
{
	import mx.collections.ArrayCollection;
	import mx.controls.DateField;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.events.MoveEvent;
	
	import spark.components.Application;
	import spark.components.DropDownList;

	/**
	 * 작업 기록
	 * 2012. 6. 12. / 이규광 / 최초 작성.
	 */
	/**
	 * 내용.
	 * uicontrol들을 제어한다.
	 * 
	 * @productversion Flex 4.5
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 10.2
	 * @author 이규광.
	 */
	public class UIController
	{
		
		/**
		 * main application을 리턴한다.
		 */
		public static function get main():Application{
			return FlexGlobals.topLevelApplication as Application;
		}
		
		
		/**
		 * popup이 화면 범위 밖으로 나가는 것을 제한하는 이벤트 리스너를 등록
		 */
		public static function addPopupRangeLimit(comp:UIComponent):void{
			comp.addEventListener(MoveEvent.MOVE, popupRangeLimit);
			comp.addEventListener(CloseEvent.CLOSE, popupCloseHandler);
		}
		
		
		/**
		 * popup이 화면 범위 밖으로 나가는 것을 제한.
		 */
		protected static function popupRangeLimit(event:MoveEvent):void
		{
			var main:Application = main;
			
			if(event.currentTarget.x < 0)
			{
				event.currentTarget.x = 0;
			}
			else if(main.width < event.currentTarget.x + event.currentTarget.width)
			{
				event.currentTarget.x = main.width - event.currentTarget.width;
			}
			
			if(event.currentTarget.y < 0)
			{
				event.currentTarget.y = 0;
			}
			else if(main.height < event.currentTarget.y + event.currentTarget.height)
			{
				event.currentTarget.y = main.height - event.currentTarget.height;
			}
		}
		
		protected static function popupCloseHandler(event:CloseEvent):void
		{
			event.currentTarget.removeEventListener(MoveEvent.MOVE, popupRangeLimit);
			event.currentTarget.removeEventListener(CloseEvent.CLOSE, popupCloseHandler);
		}
		
		
		/**
		 * 생성자
		 */
		public function UIController()
		{
		}
		
		public static function searchComboboxLabelFun(targetCombobox:DropDownList,targetData:Object,
											   targetComboboxCode:String = "",targetArrayCode:String = ""):void{
			var targetArray:ArrayCollection = new ArrayCollection();
			var targetComboboxDataProvider:ArrayCollection = targetCombobox.dataProvider as ArrayCollection;
			var selectIndex:int	= 0;
			if(targetData is ArrayCollection){
				targetArray = targetData as ArrayCollection;
			}
			
			if(targetArrayCode){
				targetArray.addItem(targetData);
			}
			
			for(var i:int = 0; i< targetComboboxDataProvider.length; i++){
				if(targetArray.length > 0){
					for(var j:int = 0; j < targetArray.length; j++){
						if(targetComboboxDataProvider[i][targetComboboxCode] == targetArray[j][targetArrayCode]){
							selectIndex = i;
						}
					}
				}else{
					if(targetComboboxDataProvider[i][targetComboboxCode] == targetData){
						selectIndex = i;
					}
				}
			}
			targetCombobox.selectedIndex = selectIndex;
		}
		
		public static function searchDateFieldFun(targetDateField:DateField,targetObj:Object,date:String):void{
			if(targetObj[date]){
				targetDateField.text = targetObj[date];
			}else{
				targetDateField.text = "";
			}
		}
	}
}